package com.neusoft.training.podcast.service.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.training.podcast.entity.Favourite;
import com.neusoft.training.podcast.entity.User;
import com.neusoft.training.podcast.entity.Video;
import com.neusoft.training.podcast.mapper.FavouriteMapper;
import com.neusoft.training.podcast.mapper.UserMapper;
import com.neusoft.training.podcast.mapper.VideoMapper;
import com.neusoft.training.podcast.service.FavouriteService;

@Service
public class FavouriteServiceImpl implements FavouriteService {
	@Autowired
	private FavouriteMapper favouritemapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired 
	private VideoMapper videoMapper;
	
	
	
	@Override
	public String addFavourite(Integer userID, String videoName) {
		String result = "";
		List<Video> video= videoMapper.findVideoByName(videoName);
		List <Favourite> favourite = favouritemapper.findFavouriteByUserVideoID(userID,video.get(0).getVideoID());
		if(!(favourite.isEmpty()))
		{
			result = "repeat";
		}
		else{
//			System.out.println(video.getVideoID());
//			java.util.Date date=null;
//			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			try {
//				date=formatter.parse(time);
//			}catch(Exception e)
//			{
//				e.printStackTrace();
//			}
//		
//			java.sql.Date sqldate=new java.sql.Date(date.getTime());
			Timestamp ts = new Timestamp(System.currentTimeMillis());  
			favouritemapper.addFavourite(userID,video.get(0).getVideoID(),ts);
			result = "success";
		}
		return result;
		
	}

	@Override
	public void deleteFavourite(String FavouriteName) {
		List<Video> video = videoMapper.findVideoByName(FavouriteName);
		favouritemapper.deleteFavouriteByVedioID(video.get(1).getVideoID());// TODO Auto-generated method stub
		
	}

	@Override
	public List<Favourite> findFavouriteByUser(String userName) {
		User user = userMapper.findByName(userName);
		return favouritemapper.findFavouriteByUser(user.getUserID());// TODO Auto-generated method stub
		
	}

	@Override
	public List<Favourite> findFavouriteByVedioID(String videoName) {
		List<Video> video = videoMapper.findVideoByName(videoName);
		
		return favouritemapper.findFavouriteByVedioID(video.get(1).getVideoID());// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteFavourites(List<String> list) {
		for(int i=0;i<list.size();i++)
		{
			List<Video> video = videoMapper.findVideoByName(list.get(i));
			favouritemapper.deleteFavouriteByVedioID(video.get(1).getVideoID());
		}// TODO Auto-generated method stub
		
	}

	@Override
	public List<Video> find(String time) {
		List<Video> video = videoMapper.findVideoByName(time);
		return video;// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Favourite> findFavouriteByUserID(Integer userID) {
	
		return favouritemapper.findFavouriteByUser(userID);// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Favourite> findFavouriteByUserName(String userName) {
		User user = userMapper.findByName(userName);
		return favouritemapper.findFavouriteByUser(user.getUserID());// TODO Auto-generated method stub
		
	}


}
      